home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 14 / Mac Magazin and MacEasy Magazine CD - Issue 14.iso / Wissenschaft & Technik / Caveman Sound / sndtest.c < prev    next >
C/C++ Source or Header  |  1995-09-11  |  5KB  |  146 lines

  1. /*****************************************************************************
  2.  * FILE:        sndtest.c
  3.  * AUTHOR:        David Hay
  4.  * CREATED:        March 9, 1995
  5.  * DESCRIPTION:    Test program and tutorial for using the Caveman Sound System.
  6.  *
  7.  * Copyright © 1995 David Hay
  8.  *
  9.  * Permission to use, copy, and distribute this software and its documentation
  10.  * for any purpose is hereby granted without fee,  provided that (i) the above
  11.  * copyright notices  and  this permission notice  appear in all copies of the
  12.  * software  and  related documentation,  and (ii) the names of David Hay  and
  13.  * Caveman Creations may not be used in any advertising or publicity  relating
  14.  * to the software without the specific, prior written permission of David Hay
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  17.  * IMPLIED  OR  OTHERWISE,  INCLUDING,  WITHOUT  LIMITATION,  ANY WARRANTY  OF
  18.  * MERCHANTABILITY  OR  FITNESS FOR  A PARTICULAR PURPOSE.  IN NO EVENT  SHALL
  19.  * DAVID HAY  OR  CAVEMAN CREATIONS  BE LIABLE  FOR  ANY SPECIAL,  INCIDENTAL,
  20.  * INDIRECT  OR  CONSEQUENTIAL DAMAGES OF ANY KIND,  OR ANY DAMAGES WHATSOEVER
  21.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS,  WHETHER OR NOT ADVISED OF THE
  22.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *****************************************************************************/
  25.  
  26. #ifndef __SOUND__
  27. #include <Sound.h>
  28. #endif
  29. #ifndef __CMSOUNDSYSTEM__
  30. #include "CMSoundSystem.h"
  31. #endif
  32.  
  33.  
  34. #define kNumChannels    1        /*  The number of sound channels to open  */
  35.  
  36.  
  37. /*  Standard Mac Toolbox initialization  */
  38. static void
  39. InitMacintosh( void )
  40. {
  41.     MaxApplZone();
  42.     
  43.     InitGraf(&qd.thePort);
  44.     InitFonts();
  45.     FlushEvents(everyEvent, 0);
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(0L);
  50.     InitCursor();
  51. }
  52.  
  53. void main( void )
  54. {
  55.     WindowPtr win;
  56.     short    ii;
  57.     Str255    name;
  58.     Handle    icon;
  59.     short    sndRef;
  60.     OSErr    err;
  61.     
  62.     InitMacintosh();
  63.     
  64.         /*  First you must initialize the sound system by specifying the
  65.         **  number of channels you wish to use.
  66.         **/
  67.     err = CMSInitSound( kNumChannels );
  68.     
  69.         /*  After initialization, each channel must be opened for use.
  70.         **  These can be opened individually with CMSOpenChannel() or
  71.         **  you may open them all, as we have done here, with a call
  72.         **  to CMSOpenAllChannels().
  73.         **/
  74.     if ( err == noErr )    err = CMSOpenAllChannels();
  75.         
  76.         /*  First let's just play a sound resource.  This behaves almost
  77.         **  identically to SndPlay().  You specify the resource number,
  78.         **  the channel to play the sound on (sound channels start at 0)
  79.         **  and whether the sound should be played asynchronously.
  80.         **/
  81.     if ( err == noErr )    err = CMSPlaySoundResource( 128, 0, true );
  82.     
  83.         /*  Since we played the last sound asynchronously, we want to
  84.         **  find out when it stops so we can do other stuff.  To do
  85.         **  this we use the CMSGetSoundPlaying() routine.  This returns
  86.         **  the sound reference number of the currently playing sound,
  87.         **  kSoundResource or kNoSound in the sndRef paramater.  I
  88.         **  could have used the routine CMSWaitForSilence() here, but
  89.         **  very often the sound was played asynchronously so you could
  90.         **  do other stuff while the sound is playing.  Plus I wanted to
  91.         **  show how the CMSGetSoundPlaying() function worked.  ;-)
  92.         **/
  93.     while ( err == noErr )
  94.     {
  95.         err = CMSGetSoundPlaying( 0, &sndRef );
  96.         if ( sndRef == kNoSound )
  97.             break;
  98.     }
  99.     
  100.         /*  We call CMSIdleSoundTask() now to unlock the sound handle
  101.         **  that we played with CMSPlaySoundResource().  This allows it
  102.         **  to be purged if necessary.  If we had called CMSStopSound()
  103.         **  this would have been done for us automatically.  This is useful
  104.         **/
  105.     CMSIdleSoundTask();
  106.     
  107.     
  108.         /*  Now let's play some music.  First call CMSLoadMusic() to
  109.         **  load the music blocks into memory and register them with
  110.         **  the sound system.
  111.         **/
  112.     if ( err == noErr )    err = CMSLoadMusic( 128 );
  113.     
  114.     if ( err == noErr )
  115.     {
  116.             /*  Start the music going with CMSPlayMusic().  Here we indicate
  117.             **  that the music should be played on sound channel 0.  This
  118.             **  will play the music asynchronously.  If a loop has been
  119.             **  defined in the 'MUSL' resource, then the music will loop
  120.             **  until it is stopped with a call to CMSStopSound() or another
  121.             **  sound is played on the same channel.
  122.             **/
  123.         err = CMSPlayMusic( 0 );
  124.         
  125.         while ( err == noErr )
  126.         {
  127.             err = CMSGetSoundPlaying( 0, &sndRef );
  128.             if ( sndRef < 1 )
  129.                 break;
  130.         }
  131.             
  132.         while ( !Button() && err == noErr )
  133.         {}
  134.     }
  135.     
  136.     if ( err == noErr )    CMSStopSound( 0 );
  137.     
  138.         /*  We're finished with the sound system, so call CMSDisposeSound()
  139.         **  to close and free the sound channels and any sounds registered
  140.         **  with the sound system.
  141.         **/
  142.     CMSDisposeSound();
  143.     
  144.     FlushEvents( everyEvent, 0 );
  145.     if ( err != noErr )    SysBeep( 10 );
  146. }